<?php

$thisPage = "register";
$subpage = getRequestParameter("subpage", false);

TemplateHelper::useLayout("form");

Log::info("Loading page functions: $thisPage");

addDataService("doregister");
addDataService("userexists");
addDataService("ack");
addDataService("unsubscribe");

function executeService($serviceName, $params) {
    switch ($serviceName) {
        case "userexists" : { return service_userexists($params); }
        case "doregister" : { return service_doregister($params); }
        case "ack" : { return service_ack($params); }
        case "unsubscribe" : { return service_unsubscribe($params); }
    }
}

function buildLayout(&$smarty) {
    global $subpage;

    Log::debug("Executing function: buildLayout");
    if ($subpage === false) {
        $smarty->assign("form", getRegisterFormData($smarty));
    } else if ($subpage == "success") {
        $smarty->assign("form", getSuccessContent($smarty));
    } else if ($subpage == "ackfail") {
        $smarty->assign("form", getAckFailContent($smarty));
    } else if ($subpage == "acknowledged") {
        $smarty->assign("form", getAckSuccessContent($smarty));
    }
}

function service_userexists($params) {
    Log::info("executing service: userexists");
    $exists = false;
    $success = true;
    $r = "";
    try {
        Database::loadModel("user");
        $exists = Database::getModel()->userExists($params['username']);
    }
    catch (Exception $e) {
        $success = false;
        $errorMsg = $e->getMessage();
    }
    $result = array(
        exists => $exists,
    );
    Log::info("Finishing service: userexists");
    if ($success) {
        $r = serviceSuccessful($result);
    } else {
        $r = serviceFailure($result, $e->getMessage());
    }
    return $r;
}

function service_unsubscribe($params) {
}

function service_doregister($params) {
    global $thisPage;

    Log::info("executing service: doregister");
    $success = true;
    $result = "";
    $errorMsg = "";
    $success = true;
    $captcha = getCaptchaObject();
    if (!$captcha->validate($params['random'])) {
        $errorMsg = "Captcha session key does not exist.";
        $success = false;
    } else if (!$captcha->verify($params['captcha'])) {
        $errorMsg = "You have entered a wrong captcha code.";
        $success = false;
    } else {
        try {
            Database::loadModel("user");
            $userdata = sophisticateData($params);
            // ez azert kell, mert egyesult a selling es a buying leads
            $userdata['registertype'] = ($userdata['registertype'] > 3 ? $userdata['registertype'] + 1 : $userdata['registertype']);
            $userdata['ticket'] = substr(md5(rand() % 10000), 0, 6);
            Database::addNew($userdata);
            $params['link'] = buildServiceURL($thisPage, "ack", array(
                ticket => $userdata['ticket']
            ));
            sendEmail("register_validation", $userdata['email'], $params);
        }
        catch (Exception $e) {
            $success = false;
            $errorMsg = $e->getMessage();
        }
    }
    if ($success) {
        Log::info("Finished service: doregister status: success");
        $result = serviceSuccessfulWithRedirect(array(), "register", "success");
    } else {
        Log::info("Finished service: doregister status: failure");
        $result = serviceFailure(array(), $errorMsg);
    }
    return $result;
}

function service_ack($params) {
    Log::info("executing service: ack");
    $success = true;
    try {
        Database::loadModel("user");
        $success = Database::getModel()->evaluate($params['ticket']);
    }
    catch (Exception $e) {
        $success = false;
    }
    if ($success) {
        Log::info("Finished service: ack status: success");
        $result = redirect("register", "acknowledged");
    } else {
        Log::info("Finished service: ack status: failure");
        $result = redirect("register", "ackfail");
    }
    return $result;
}

function getPageHeader() {
    Log::debug("Executing function: getPageHeader");
    return TemplateHelper::getDefaultPageHeader();
}

function getPageFooter() {
    Log::debug("Executing function: getPageFooter");
    return TemplateHelper::getDefaultPageFooter();
}

function getRegisterFormData(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getRegisterFormData");
    $smarty->assign("captcha", TemplateHelper::addCaptchaService());
    return array(
        title => "Registration",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "register_content")),
        style => "largeform",
    );
}

function getSuccessContent(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getSuccessContent");
    return array(
        title => "Registration",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "register_success")),
    );
}

function getAckFailContent(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getAckFailContent");
    return array(
        title => "Acknowledge failed",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "register_ackfail")),
    );
}

function getAckSuccessContent(&$smarty) {
    global $thisPage;
    Log::debug("Executing function: getAckSuccessContent");
    return array(
        title => "Acknowledge successful",
        content => $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "register_acksuccess")),
    );
}

?>
